package container;

import java.io.File;
import java.util.List;

public class Host extends Mapper<ContextList> {
    /**
     * 进行创建的时候将里面的servlet全部进行初始化
     * @param appBase
     */
    public Host(String appBase){
        this.path = appBase;
        File baseFile = new File(appBase);
        File[] files = baseFile.listFiles();
        this.obj = new ContextList();
        for (File file : files) {
            obj.getContexts().add(new Context(file,file.getName()));
        }
    }

    /**
     * /demo1/lagou
     * 这个对象如果获取到Servlet,那么执行Servlet,如果获取不到Servlet,那么返回字符串/null
     * @return
     */
    public  Object getResolver(String url){
        List<Context> contexts = obj.getContexts();
        String[] paths = url.split("/");
        for (Context context : contexts) {
            String path = context.getPath();
            //这里就取到了Context
            if(path.equals(paths[1])){
                WrapperList wrapperList = context.getObj();
                List<Wrapper> wrappers = wrapperList.getWrappers();
                for (Wrapper wrapper : wrappers) {
                    if(wrapper.getPath().equals("/"+paths[2])){
                        return wrapper.getObj();
                    }
                }
            }
        }
        return getPath() + url;
    }

}
